package wineshop.fixture;

import javax.naming.NamingException;

import wineshop.domain.market.Customer;
import wineshop.test.CustomerBeanITRemote;

public class CustomerManagement {
	private CustomerBeanITRemote customerBean;
	private Customer lastCustomerCreated;

	public CustomerManagement() throws NamingException {
		customerBean = (CustomerBeanITRemote) Environment.context.lookup("wineshop.test.CustomerBeanITRemote");
	}

	public boolean noUserExistsWithUsername(String username) {
		Customer customer = customerBean.getCustomerByUserName(username);
		return customer == null;
	}

	public void createANewUserWithNameAndSurnameAndPassword(String username, String name, String surname, String password) {
		Customer customer = new Customer(name, surname, username);
		try {
			customerBean.createCustomer(customer, password.toCharArray());
			lastCustomerCreated = customer;
		} catch (Exception e) {
			lastCustomerCreated = null;
		}
	}

	public String userCreated() {
		if (lastCustomerCreated == null) {
			return "none";
		}
		return lastCustomerCreated.getUsername();
	}
	
}
